package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "订单管理")
@Slf4j
@RequestMapping("/admin/order")
@RestController("adminOrderController")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("---管理端---订单搜索：查询条件{}",ordersPageQueryDTO);
        PageResult ordersList = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(ordersList);
    }

    @ApiOperation("订单状态统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getOrderStatistics(){
        OrderStatisticsVO orderStatisticsVO =  orderService.getOrderStatistics();
        return Result.success(orderStatisticsVO);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> queryOrderDetail(@PathVariable Long id){
        log.info("---S端---服务端查询订单详情，订单id:{}",id);
        OrderVO orderVO = orderService.queryOrderDetail(id);
        return Result.success(orderVO);
    }

    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result acceptOrders(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("---S端---服务端接单，订单id:{}",ordersConfirmDTO);
        orderService.acceptOrders(ordersConfirmDTO);
        return Result.success();
    }

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejectOrders(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("---S端---服务端拒单，拒单原因:{}",ordersRejectionDTO);
        orderService.rejectOrders(ordersRejectionDTO);
        return Result.success();
    }

    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result deliveryOrders(@PathVariable Long id){
        log.info("---S端---给骑手派单，订单id：{}",id);
        orderService.deliveryOrders(id);
        return Result.success();
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancelOrders(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("---S端---取消订单：{}",ordersCancelDTO);
        orderService.adminCancelOrders(ordersCancelDTO);
        return Result.success();
    }

    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result completeOrders(@PathVariable Long id){
        log.info("---S端---完成订单:{}",id);
        orderService.completeOrders(id);
        return Result.success();
    }
}
